######
#' @TODO 差异基因染色体图谱标记结果展示
#' @title ## 差异基因染色体图谱标记结果展示
#' @description 利用gencode.v38.annotation.gtf文件注释基因位置，然后标记出差异基因中显著的兴趣基因。  
#' @details 具体描述可参考：蓝色线条表示下调的差异表达基因，红色线条表示上调的差异表达基因，蓝色方块表示下调的差异表达炎症反应相关基因，红色圆形表示上调的差异表达炎性反应基因
#'
#'  @param deg_res 差异基因结果，可以是筛选后的结果。
#' 需要有log2FC做列名，gene做列名。
#' @param mark_gene 为图谱中需要标记出的基因，如差异基因中的炎症因子，或者缺氧因子等等。
#' 
#' > head(deg1)
#'      gene    log2FC       pvalue adjusted_pvalue
#' 1 MTHFD1L -1.579919 1.364479e-39    7.966784e-35
#' 2    MYOC  3.366953 9.695933e-39    2.830582e-34
#' 3  CLEC3B  2.734053 1.611884e-38    3.137102e-34
#' 4    RCC2 -1.652020 2.185500e-36    2.552096e-32
#' 5  NFE2L3 -2.196941 9.787538e-35    7.143312e-31
#' 6    FIGF  1.519971 4.659633e-34    3.022911e-30
#' @usage a2 <- DEgene_chr_display(deg_res = degs_sig ,mark_gene = pathway$KEGG_ARGININE_AND_PROLINE_METABOLISM,output_dir = output_dir,var_name = 'HNSC' )
#' @param return_file_style null
#' @param return_file output_dir生成pdf文件
#' @param output_dir 结果输出路径 需要以/结尾
#' @param var_name 字符串，用于命名文件夹与文件
#' 
#' @author *WYK*
######
DEgene_chr_display <- function(deg_res = NULL, mark_gene = NULL, output_dir = "./",
    var_name = NULL) {

    if (is.null(var_name)) {
        var_name <- paste0("_", paste0(sample(letters, 4), collapse = ""))
    }

    require(RIdeogram)
    require(rtracklayer)

    data(human_karyotype, package = "RIdeogram")
    data(gene_density, package = "RIdeogram")

    if (!file.exists("/Pub/Users/wangyk/Project_wangyk/Codelib_YK/some_scr/gene_pos.RData")) {
        gene_pos <- rtracklayer::import("gencode.v38.annotation.gtf") %>%
            as.data.frame() %>%
            filter(type == "gene") %>%
            dplyr::select(seqnames, start, end, gene_name)

        save(gene_pos, file = "/Pub/Users/wangyk/Project_wangyk/Codelib_YK/some_scr/gene_pos.RData")
    } else {
        load("/Pub/Users/wangyk/Project_wangyk/Codelib_YK/some_scr/gene_pos.RData",
            verbose = T)
    }

    a <- gene_pos[na.omit(match(deg_res$gene, gene_pos$gene_name)), ]

    a[, 1] <- gsub(pattern = "chr", replacement = "", x = as.character(a$seqnames))

    colnames(a)[4] <- "gene"

    a <- left_join(a, deg_res)

    a[, "Type"] <- ifelse(a$log2FC > 0, "Up", "Down")
    a[, "color"] <- ifelse(a$log2FC > 0, "CC0202", "2D5689")
    a[, "Shape"] <- ifelse(a$log2FC > 0, "circle", "box")
    colnames(a)[c(1, 2, 3)] <- c("Chr", "Start", "End")

    print(head(a, 2))

    all_degs <- a %>%
        dplyr::select(Chr, Start, End, log2FC) %>%
        dplyr::rename(Value = log2FC) %>%
        mutate(Value = ifelse(Value > 0, 50, 0))

    labels_pos <- a %>%
        filter(gene %in% mark_gene) %>%
        dplyr::select(Type, Shape, Chr, Start, End, color)

    data(human_karyotype, package = "RIdeogram")

    if (!dir.exists(sprintf("%soutput/gene_chr_pos_%s", output_dir, var_name))) {
        dir.create(sprintf("%soutput/gene_chr_pos_%s", output_dir, var_name), showWarnings = F,
            recursive = T)
    } else {
        print(sprintf("Dir '%soutput/gene_chr_pos_%s' is existed.", output_dir, var_name))
    }
    dir_now <- sprintf("%soutput/gene_chr_pos_%s/", output_dir, var_name)

    ideogram(karyotype = human_karyotype, label = labels_pos, overlaid = all_degs,
        colorset2 = c("#377EB8", "#E41A1C"), colorset1 = c("#377EB8", "#FFFFBF",
            "#E41A1C"), label_type = "marker", width = 180, output = str_glue("{dir_now}chromosome.svg"))
    convertSVG(svg = str_glue("{dir_now}chromosome.svg"), file = sprintf("gene_chr_pos_%s",
        var_name), device = "pdf")
    convertSVG(svg = str_glue("{dir_now}chromosome.svg"), file = sprintf("gene_chr_pos_%s",
        var_name), device = "png", dpi = 300)
}



